<template>
  <!-- 财务  财务账单核验 -->
  <div class="app-container jklis" v-loading="loading">
    <el-form :model="queryParams" ref="queryForm" :inline="true" class="zdy-form search-form-new" label-width="100px">
      <el-form-item label="核验状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择核验状态" size="small" clearable>
          <el-option v-for="(dict, key) in commonMaps.billStateMap" :key="key" :label="dict.label" :value="key" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" size="small" clearable>
          <el-option v-for="(dict, key) in commonMaps.tenantBillOrderTypeMap" :key="key" :label="dict" :value="key" />
        </el-select>
      </el-form-item>
      <el-form-item label="账单类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择账单类型" size="small" clearable>
          <el-option v-for="(dict, key) in commonMaps.billTypeMap" :key="key" :label="dict.label" :value="key" />
        </el-select>
      </el-form-item>
      <el-form-item label="交易方名称" prop="jiaoyiName">
        <el-input v-model="queryParams.jiaoyiName" placeholder="请输入交易方名称" clearable />
      </el-form-item>
      <el-form-item label="订单搜索" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单搜索" clearable />
      </el-form-item>
      <el-form-item label="创建日期" prop="">
        <el-date-picker v-model="timer2" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="核验日期" prop="">
        <el-date-picker v-model="timer" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" icon="el-icon-search" @click="searchList" class="zdy-btn">搜索</el-button>
        <el-button icon="el-icon-refresh" type="primary" plain @click="resetQuery" class="zdy-btn zdy-btn-refresh">重置</el-button>
        <el-button v-if="checkPermi(['billAudit:audit'])" icon="el-icon-download" type="success" @click="importExl">导出</el-button>
        <el-button :disabled="selectRows.length==0" v-if="checkPermi(['billAudit:audit'])" icon="el-icon-check" type="warning" @click="piliangheyan">批量核验</el-button>
      </el-form-item>
    </el-form>
    <div class="table_box">
      <el-table :data="tableData.records" border @selection-change="handleSelectionChange">
        <!-- <el-table-column
          prop=""
          label="归属部门"
          min-width="120"
        ></el-table-column> -->
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160"></el-table-column>
        <el-table-column prop="orderType" label="订单类型" width="110">
          <template slot-scope="scope">
            <div>
              {{ commonMaps.tenantBillOrderTypeMap[scope.row.orderType] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="orderNo" label="订单详情" width="200">
          <template slot-scope="scope">
            <span class="link" @click="clickOrderNo(scope.row)">
              {{ scope.row.orderNo }}
            </span>
          </template>

        </el-table-column>
        <!-- <el-table-column
          prop="no"
          label="账单编号"
          min-width="120"
        ></el-table-column> -->
        <el-table-column prop="type" label="账单类型" width="100">
          <template slot-scope="scope">
            <div>
              <el-tag :type="commonMaps.billTypeMap[scope.row.type].type">{{ commonMaps.billTypeMap[scope.row.type].label }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="金额" width="100"></el-table-column>
        <el-table-column prop="jiaoyiName" label="交易方"> </el-table-column>
        <el-table-column prop="otherNo" label="三方单号"> </el-table-column>
        <el-table-column prop="operatorName" label="核验人"></el-table-column>
        <el-table-column prop="state" label="状态" width="120">
          <template slot-scope="scope">
            <div>
              <el-tag type="danger" v-if="scope.row.state==1">未核验</el-tag>
              <el-tag type="success" v-if="scope.row.state==2">已核验</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="operatorTime" label="核验时间" width="160"></el-table-column>
        <el-table-column prop="img" label="核验附件" width="100">
          <template slot-scope="scope">

            <div v-if="scope.row.img" class="link" @click="viewImgs(scope.row)">查看附件</div>
            <div v-else>暂无附件</div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button v-if="scope.row.state==1&&checkPermi(['billAudit:audit'])" type="text" @click="openDakuan(scope.row)">立即核验</el-button>
            <!-- <el-button type="del" @click="jujueDakuan(scope.row)">拒绝打款</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pages-box">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParams.pageNum" :page-sizes="[10,20,30, 50]" :page-size="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total"></el-pagination>
    </div>

    <imageDialog ref="imageDialog"></imageDialog>
    <dakuanDialog ref="dakuanDialog"></dakuanDialog>
    <!-- 各种业务订单弹窗 -->
    <backOrderDetailDialog ref="backOrderDetailDialog"></backOrderDetailDialog>
    <JiZhengBenDetail ref="JiZhengBenDetail"></JiZhengBenDetail>
    <saleOrderDetailDialog ref="saleOrderDetailDialog"></saleOrderDetailDialog>
    <afterSellView ref="afterSellView"></afterSellView>
    <peifuView ref="peifuView"></peifuView>

    <rentalOrderDetails ref="rentalOrderDetailRef"></rentalOrderDetails>

  </div>
</template>

<script>
import billListApi from "@/api/financeCenter/billList.js";
import imageDialog from "@/components/imageDialog.vue";
import dakuanDialog from "./auditDialog.vue";

// 业务订单弹窗
import backOrderDetailDialog from "../recycleManage/components/orderDetailDialog.vue";
import JiZhengBenDetail from "../tools/jizhangben/detail.vue";
import saleOrderDetailDialog from "../saleOrder/components/orderDetailDialog.vue";
import afterSellView from "../saleAfterOrder/components/afterSellView.vue";
import peifuView from "../peifuOrder/components/peifuView.vue";
import rentalOrderDetails from "@/views/rental/components/rentalOrderDetail.vue";

import { rentalOrderDetail } from "@/api/rental/account.js";

export default {
  name: "BillAudit",
  components: {
    imageDialog,
    dakuanDialog,

    backOrderDetailDialog,
    JiZhengBenDetail,
    saleOrderDetailDialog,
    afterSellView,
    peifuView,
    rentalOrderDetails
  },
  data() {
    return {
      timer: [],
      timer2:[],
      // 提号时间，商品ID 游戏类型 区服 实名状态   游戏账号【对应的商品链接】
      tableData: {
        records: [],
        total: 0,
      },
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operatorTimeStart: "",
        operatorTimeEnd: "",
        state: "1",
      },
      selectRows: [],
    };
  },
  created() {
    this.getList();
  },
  activated() {},
  methods: {
    // 获取列表
    getList() {
      // queryParams 请求列表
      this.loading = true;
      if (this.timer && this.timer.length == 2) {
        this.queryParams.operatorTimeStart = this.timer[0] + " 00:00:00";
        this.queryParams.operatorTimeEnd = this.timer[1] + " 23:59:59";
      } else {
        this.queryParams.operatorTimeStart = "";
        this.queryParams.operatorTimeEnd = "";
      }
      if (this.timer2 && this.timer2.length == 2) {
        this.queryParams.createTimeStart = this.timer2[0] + " 00:00:00";
        this.queryParams.createTimeEnd = this.timer2[1] + " 23:59:59";
      } else {
        this.queryParams.createTimeStart = "";
        this.queryParams.createTimeEnd = "";
      }
      billListApi.getTenantAuditBillList(this.queryParams).then((res) => {
        this.loading = false;
        this.tableData.records = res.rows;
        this.tableData.total = res.total;
      });
    },
    // 搜索
    searchList() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 导出
    importExl() {
      this.loading = true;
      if (this.timer && this.timer.length == 2) {
        this.queryParams.operatorTimeStart = this.timer[0] + " 00:00:00";
        this.queryParams.operatorTimeEnd = this.timer[1] + " 23:59:59";
      } else {
        this.queryParams.operatorTimeStart = "";
        this.queryParams.operatorTimeEnd = "";
      }
      if (this.timer2 && this.timer2.length == 2) {
        this.queryParams.createTimeStart = this.timer2[0] + " 00:00:00";
        this.queryParams.createTimeEnd = this.timer2[1] + " 23:59:59";
      } else {
        this.queryParams.createTimeStart = "";
        this.queryParams.createTimeEnd = "";
      }
      billListApi
        .tenantAuditExport(this.queryParams)
        .then((res) => {
          this.$download.downloadFile(res, "账单导出");
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 选择行 变化
    handleSelectionChange(e) {
      this.selectRows = e;
    },
    // 批量核验
    piliangheyan() {
      if (this.selectRows && this.selectRows.length) {
        this.$confirm(
          `确认通过核验选中的${this.selectRows.length}个订单吗`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            this.loading = true
            const idArr = this.selectRows.map((r) => r.id);
            billListApi.tenantAuditHeyanPiliang({
              idList:idArr
            }).then(res=>{
              this.getList();
              this.loading = false
            })
          })
          .catch(() => {});
      } else {
        return this.$message.error("请选择要核验的订单！");
      }
    },
    //  点击业务单号
    clickOrderNo(item) {
      if (item.orderType == 1) {
        // 销售订单
        this.$refs.saleOrderDetailDialog.open(item.businessId, "kefu");
      } else if (item.orderType == 2) {
        // 销售售后
        this.$refs.afterSellView.open(item.businessId);
      } else if (item.orderType == 3) {
        // 回收订单
        this.$refs.backOrderDetailDialog.open(item.businessId);
      } else if (item.orderType == 4) {
        // 回收售后
      } else if (item.orderType == 5) {
        // 采购订单
        this.$refs.saleOrderDetailDialog.open(item.businessId, "buyer2");
      } else if (item.orderType == 6) {
        // 采购售后
        this.$refs.afterSellView.open(item.businessId);
      } else if (item.orderType == 7) {
        // 日常收支
        this.$refs.JiZhengBenDetail.open(item.businessId);
      } else if (item.orderType == 8) {
        // 订单赔付
        this.$refs.peifuView.open(item.businessId, "buyer");
      }else if (item.orderType == 9) {
           // 租号订单
        rentalOrderDetail(item.businessId).then(res=> {
          this.$refs.rentalOrderDetailRef.init(res.data)
        })
     
      }
    },
    // openDakuan 打开打款弹窗
    openDakuan(row) {
      this.$refs.dakuanDialog.open(row);
    },

    // 打开查看图片弹窗
    viewImgs(row) {
      if (row.img) {
        let list = row.img.split(",");
        if (list.length) {
          this.$refs.imageDialog.open(list);
        } else {
          this.$alert("暂无图片");
        }
      } else {
        this.$alert("暂无图片");
      }
    },

    resetQuery() {
      this.timer = [];
      this.timer2 = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        operatorTimeStart: "",
        operatorTimeEnd: "",
        createTimeStart:'',
        createTimeEnd:'',
        state: "1",
      };
      this.getList();
    },

    // 每页条数改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    // 页码改变
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    exportOrder() {},
  },
};
</script>
<style lang="scss" scoped>
.link {
  color: blue;
  cursor: pointer;
}
</style>
